package panels;

import javax.swing.JPanel;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JCheckBox;
import java.awt.BorderLayout;
import javax.swing.JButton;
import sqlmanager.FieldInfo;
import utility.SelectAllWhenGainFocus;
import java.awt.FlowLayout;
import java.awt.event.KeyEvent;

public class FieldEditorPanel extends JPanel {
	private JPanel generalPanel = null;
	private JLabel nameLabel = null;
	private JTextField nameTextField = null;
	private JLabel positionLabel = null;
	private JLabel typeLabel = null;
	private JComboBox positionComboBox = null;
	private JLabel lenthLabel = null;
	private JTextField lenthTextField = null;
	public JComboBox typeComboBox = null;
	private JCheckBox notNullCheckBox = null;
	private JCheckBox primaryKeyCheckBox = null;
	private JLabel defaultLabel = null;
	private JTextField defaultTextField = null;
	private JTextField scaleTextField = null;
	private JPanel buttonsPanel = null;
	private JPanel buttons2Panel = null;
	public JButton hideButton = null;
	public JButton cancelButton = null;
	public JButton applyButton = null;
	private JCheckBox autoIncreamentCheckBox = null;

	private SelectAllWhenGainFocus focusHandler = null;

	/**
	 * This is the default constructor
	 */
	public FieldEditorPanel() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		GridBagConstraints gridBagConstraints21 = new GridBagConstraints();
		gridBagConstraints21.fill = java.awt.GridBagConstraints.BOTH; // Generated
		gridBagConstraints21.weighty = 10.0; // Generated
		gridBagConstraints21.gridx = 0; // Generated
		gridBagConstraints21.gridy = 1; // Generated
		gridBagConstraints21.weightx = 10.0; // Generated
		GridBagConstraints gridBagConstraints = new GridBagConstraints();
		gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; // Generated
		gridBagConstraints.weighty = 0.0; // Generated
		gridBagConstraints.insets = new java.awt.Insets(6, 6, 3, 5); // Generated
		gridBagConstraints.ipadx = 0; // Generated
		gridBagConstraints.ipady = 0; // Generated
		gridBagConstraints.weightx = 10.0; // Generated
		this.setLayout(new GridBagLayout()); // Generated
		this.setSize(500, 220);
		this.setMinimumSize(new java.awt.Dimension(0, 0)); // Generated
		this.add(getGeneralPanel(), gridBagConstraints); // Generated
		this.add(getButtonsPanel(), gridBagConstraints21); // Generated
	}

	/**
	 * This method initializes generalPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getGeneralPanel() {
		if (generalPanel == null) {
			GridBagConstraints gridBagConstraints14 = new GridBagConstraints();
			gridBagConstraints14.gridx = 3; // Generated
			gridBagConstraints14.gridwidth = 2; // Generated
			gridBagConstraints14.anchor = java.awt.GridBagConstraints.WEST; // Generated
			gridBagConstraints14.insets = new java.awt.Insets(0, 2, 0, 0); // Generated
			gridBagConstraints14.fill = java.awt.GridBagConstraints.HORIZONTAL; // Generated
			gridBagConstraints14.gridy = 3; // Generated
			GridBagConstraints gridBagConstraints12 = new GridBagConstraints();
			gridBagConstraints12.fill = java.awt.GridBagConstraints.HORIZONTAL; // Generated
			gridBagConstraints12.gridy = 1; // Generated
			gridBagConstraints12.weightx = 1.0; // Generated
			gridBagConstraints12.insets = new java.awt.Insets(0, 0, 3, 4); // Generated
			gridBagConstraints12.gridx = 4; // Generated
			GridBagConstraints gridBagConstraints13 = new GridBagConstraints();
			gridBagConstraints13.anchor = java.awt.GridBagConstraints.EAST; // Generated
			gridBagConstraints13.insets = new java.awt.Insets(0, 5, 3, 0); // Generated
			GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
			gridBagConstraints11.fill = java.awt.GridBagConstraints.HORIZONTAL; // Generated
			gridBagConstraints11.gridy = 2; // Generated
			gridBagConstraints11.weightx = 1.0; // Generated
			gridBagConstraints11.gridwidth = 4; // Generated
			gridBagConstraints11.insets = new java.awt.Insets(0, 6, 0, 5); // Generated
			gridBagConstraints11.gridx = 1; // Generated
			GridBagConstraints gridBagConstraints10 = new GridBagConstraints();
			gridBagConstraints10.gridx = 0; // Generated
			gridBagConstraints10.anchor = java.awt.GridBagConstraints.EAST; // Generated
			gridBagConstraints10.insets = new java.awt.Insets(0, 5, 0, 0); // Generated
			gridBagConstraints10.gridy = 2; // Generated
			defaultLabel = new JLabel();
			defaultLabel.setDisplayedMnemonic(KeyEvent.VK_D);
			defaultLabel.setLabelFor(this.getDefaultTextField());
			defaultLabel.setText("Default"); // Generated
			GridBagConstraints gridBagConstraints9 = new GridBagConstraints();
			gridBagConstraints9.gridx = 1; // Generated
			gridBagConstraints9.gridwidth = 1; // Generated
			gridBagConstraints9.anchor = java.awt.GridBagConstraints.WEST; // Generated
			gridBagConstraints9.fill = java.awt.GridBagConstraints.HORIZONTAL; // Generated
			gridBagConstraints9.weightx = 0.0; // Generated
			gridBagConstraints9.insets = new java.awt.Insets(0, 2, 0, 0); // Generated
			gridBagConstraints9.gridy = 3; // Generated
			GridBagConstraints gridBagConstraints8 = new GridBagConstraints();
			gridBagConstraints8.gridx = 2; // Generated
			gridBagConstraints8.gridwidth = 1; // Generated
			gridBagConstraints8.fill = java.awt.GridBagConstraints.HORIZONTAL; // Generated
			gridBagConstraints8.anchor = java.awt.GridBagConstraints.WEST; // Generated
			gridBagConstraints8.insets = new java.awt.Insets(0, 2, 0, 0); // Generated
			gridBagConstraints8.weightx = 0.0; // Generated
			gridBagConstraints8.gridy = 3; // Generated
			GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
			gridBagConstraints5.fill = java.awt.GridBagConstraints.HORIZONTAL; // Generated
			gridBagConstraints5.gridy = 1; // Generated
			gridBagConstraints5.weightx = 1.0; // Generated
			gridBagConstraints5.insets = new java.awt.Insets(0, 6, 3, 0); // Generated
			gridBagConstraints5.gridx = 1; // Generated
			GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
			gridBagConstraints7.fill = java.awt.GridBagConstraints.HORIZONTAL; // Generated
			gridBagConstraints7.gridy = 1; // Generated
			gridBagConstraints7.weightx = 1.0; // Generated
			gridBagConstraints7.insets = new java.awt.Insets(0, 6, 3, 5); // Generated
			gridBagConstraints7.gridx = 3; // Generated
			GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
			gridBagConstraints6.gridx = 2; // Generated
			gridBagConstraints6.anchor = java.awt.GridBagConstraints.EAST; // Generated
			gridBagConstraints6.insets = new java.awt.Insets(0, 6, 3, 0); // Generated
			gridBagConstraints6.gridy = 1; // Generated
			lenthLabel = new JLabel();
			lenthLabel.setDisplayedMnemonic(KeyEvent.VK_L);
			lenthLabel.setLabelFor(this.getLenthTextField());
			lenthLabel.setText("Lenth / Scale"); // Generated
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.fill = java.awt.GridBagConstraints.HORIZONTAL; // Generated
			gridBagConstraints3.gridy = 0; // Generated
			gridBagConstraints3.weightx = 1.0; // Generated
			gridBagConstraints3.insets = new java.awt.Insets(0, 6, 3, 5); // Generated
			gridBagConstraints3.gridwidth = 2; // Generated
			gridBagConstraints3.gridx = 3; // Generated
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.gridx = 0; // Generated
			gridBagConstraints4.anchor = java.awt.GridBagConstraints.EAST; // Generated
			gridBagConstraints4.insets = new java.awt.Insets(0, 5, 3, 0); // Generated
			gridBagConstraints4.gridy = 1; // Generated
			typeLabel = new JLabel();
			typeLabel.setDisplayedMnemonic(KeyEvent.VK_T);
			typeLabel.setLabelFor(this.getTypeComboBox());
			typeLabel.setText("Type"); // Generated
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.gridx = 2; // Generated
			gridBagConstraints2.anchor = java.awt.GridBagConstraints.EAST; // Generated
			gridBagConstraints2.insets = new java.awt.Insets(0, 6, 3, 0); // Generated
			gridBagConstraints2.gridy = 0; // Generated
			positionLabel = new JLabel();
			positionLabel.setDisplayedMnemonic(KeyEvent.VK_P);
			positionLabel.setLabelFor(this.getPositionComboBox());
			positionLabel.setText("Position"); // Generated
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL; // Generated
			gridBagConstraints1.gridy = 0; // Generated
			gridBagConstraints1.weightx = 1.0; // Generated
			gridBagConstraints1.insets = new java.awt.Insets(0, 6, 3, 0); // Generated
			gridBagConstraints1.gridx = 1; // Generated
			nameLabel = new JLabel();
			nameLabel.setDisplayedMnemonic(KeyEvent.VK_N);
			nameLabel.setLabelFor(this.getNameTextField());
			nameLabel.setText("Name"); // Generated
			generalPanel = new JPanel();
			generalPanel.setLayout(new GridBagLayout()); // Generated
			generalPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Description",
					javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font(
							"Dialog", java.awt.Font.BOLD, 12), java.awt.Color.black)); // Generated
			generalPanel.add(nameLabel, gridBagConstraints13); // Generated
			generalPanel.add(getNameTextField(), gridBagConstraints1); // Generated
			generalPanel.add(positionLabel, gridBagConstraints2); // Generated
			generalPanel.add(typeLabel, gridBagConstraints4); // Generated
			generalPanel.add(getPositionComboBox(), gridBagConstraints3); // Generated
			generalPanel.add(lenthLabel, gridBagConstraints6); // Generated
			generalPanel.add(getLenthTextField(), gridBagConstraints7); // Generated
			generalPanel.add(getTypeComboBox(), gridBagConstraints5); // Generated
			generalPanel.add(getAutoIncreamentCheckBox(), gridBagConstraints9); // Generated
			generalPanel.add(getNotNullCheckBox(), gridBagConstraints8); // Generated
			generalPanel.add(defaultLabel, gridBagConstraints10); // Generated
			generalPanel.add(getDefaultTextField(), gridBagConstraints11); // Generated
			generalPanel.add(getScaleTextField(), gridBagConstraints12); // Generated
			generalPanel.add(getAutoIncreamentCheckBox2(), gridBagConstraints14); // Generated
		}
		return generalPanel;
	}

	/**
	 * This method initializes nameTextField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getNameTextField() {
		if (nameTextField == null) {
			nameTextField = new JTextField();
			nameTextField.addFocusListener(this.getFocusHandler());
		}
		return nameTextField;
	}

	/**
	 * This method initializes positionComboBox
	 * 
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getPositionComboBox() {
		if (positionComboBox == null) {
			positionComboBox = new JComboBox();
			positionComboBox.setPreferredSize(new java.awt.Dimension(32, 25)); // Generated
		}
		return positionComboBox;
	}

	/**
	 * This method initializes lenthTextField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getLenthTextField() {
		if (lenthTextField == null) {
			lenthTextField = new JTextField();
			lenthTextField.addFocusListener(this.getFocusHandler());
			lenthTextField.setText("30"); // Generated
			lenthTextField.setHorizontalAlignment(javax.swing.JTextField.CENTER); // Generated
			lenthTextField.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR)); // Generated
		}
		return lenthTextField;
	}

	/**
	 * This method initializes typeComboBox
	 * 
	 * @return javax.swing.JComboBox
	 */
	public JComboBox getTypeComboBox() {
		if (typeComboBox == null) {
			typeComboBox = new JComboBox();
			typeComboBox.setPreferredSize(new java.awt.Dimension(32, 25)); // Generated
			typeComboBox.setEditable(true); // Generated
		}
		return typeComboBox;
	}

	/**
	 * This method initializes notNullCheckBox
	 * 
	 * @return javax.swing.JCheckBox
	 */
	private JCheckBox getNotNullCheckBox() {
		if (notNullCheckBox == null) {
			notNullCheckBox = new JCheckBox();
			notNullCheckBox.setText("Not Null"); // Generated
			notNullCheckBox.setMnemonic(java.awt.event.KeyEvent.VK_U); // Generated
		}
		return notNullCheckBox;
	}

	/**
	 * This method initializes autoIncreamentCheckBox
	 * 
	 * @return javax.swing.JCheckBox
	 */
	private JCheckBox getAutoIncreamentCheckBox() {
		if (primaryKeyCheckBox == null) {
			primaryKeyCheckBox = new JCheckBox();
			primaryKeyCheckBox.setText("Primary Key"); // Generated
			primaryKeyCheckBox.setMnemonic(KeyEvent.VK_M); // Generated
		}
		return primaryKeyCheckBox;
	}

	/**
	 * This method initializes defaultTextField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getDefaultTextField() {
		if (defaultTextField == null) {
			defaultTextField = new JTextField();
			defaultTextField.addFocusListener(this.getFocusHandler());
		}
		return defaultTextField;
	}

	/**
	 * This method initializes decimalTextField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getScaleTextField() {
		if (scaleTextField == null) {
			scaleTextField = new JTextField();
			scaleTextField.addFocusListener(this.getFocusHandler());
			scaleTextField.setText("16"); // Generated
			scaleTextField.setHorizontalAlignment(javax.swing.JTextField.CENTER); // Generated
		}
		return scaleTextField;
	}

	/**
	 * This method initializes buttonsPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getButtonsPanel() {
		if (buttonsPanel == null) {
			buttonsPanel = new JPanel();
			buttonsPanel.setLayout(new BorderLayout()); // Generated
			buttonsPanel.add(getButtons2Panel(), java.awt.BorderLayout.SOUTH); // Generated
		}
		return buttonsPanel;
	}

	/**
	 * This method initializes buttons2Panel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getButtons2Panel() {
		if (buttons2Panel == null) {
			FlowLayout flowLayout = new FlowLayout();
			flowLayout.setVgap(6); // Generated
			flowLayout.setHgap(25); // Generated
			buttons2Panel = new JPanel();
			buttons2Panel.setLayout(flowLayout); // Generated
			buttons2Panel.add(getJButton2(), null); // Generated
			buttons2Panel.add(getJButton(), null); // Generated
			buttons2Panel.add(getHideButton(), null); // Generated
		}
		return buttons2Panel;
	}

	/**
	 * This method initializes okButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getHideButton() {
		if (hideButton == null) {
			hideButton = new JButton();
			hideButton.setText("Hide"); // Generated
			hideButton.setMnemonic(java.awt.event.KeyEvent.VK_H); // Generated
			hideButton.setPreferredSize(new java.awt.Dimension(75, 25)); // Generated
		}
		return hideButton;
	}

	/**
	 * This method initializes jButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButton() {
		if (cancelButton == null) {
			cancelButton = new JButton();
			cancelButton.setPreferredSize(new java.awt.Dimension(75, 25)); // Generated
			cancelButton.setMnemonic(java.awt.event.KeyEvent.VK_C); // Generated
			cancelButton.setText("Cancel"); // Generated
		}
		return cancelButton;
	}

	/**
	 * This method initializes jButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButton2() {
		if (applyButton == null) {
			applyButton = new JButton();
			applyButton.setPreferredSize(new java.awt.Dimension(75, 25)); // Generated
			applyButton.setMnemonic(java.awt.event.KeyEvent.VK_A); // Generated
			applyButton.setText("Apply"); // Generated
		}
		return applyButton;
	}

	/**
	 * This method initializes autoIncreamentCheckBox
	 * 
	 * @return javax.swing.JCheckBox
	 */
	private JCheckBox getAutoIncreamentCheckBox2() {
		if (autoIncreamentCheckBox == null) {
			autoIncreamentCheckBox = new JCheckBox();
			autoIncreamentCheckBox.setMnemonic(java.awt.event.KeyEvent.VK_I); // Generated
			autoIncreamentCheckBox.setText("Auto Increament"); // Generated
		}
		return autoIncreamentCheckBox;
	}

	private SelectAllWhenGainFocus getFocusHandler() {
		if (null == focusHandler)
			focusHandler = new SelectAllWhenGainFocus();
		return focusHandler;
	}

	public void setFieldInfo(FieldInfo info) {
		this.nameTextField.setText(info.name);
		this.typeComboBox.setSelectedItem(info.type);
		if (-1 != info.precision) {
			this.lenthLabel.setText("Length");
			this.lenthTextField.setText(String.valueOf(info.precision));
			if (0 != info.scale) {
				// float, numeric, etc
				this.lenthLabel.setText("Length / Scale");
				this.scaleTextField.setText(String.valueOf(info.scale));
			} else {
				this.scaleTextField.setText("");
				this.scaleTextField.setEnabled(false);
			}
		} else {
			this.lenthLabel.setEnabled(false);
			this.lenthTextField.setEnabled(false);
			this.scaleTextField.setEnabled(false);
		}
		this.primaryKeyCheckBox.setSelected(info.primaryKey);
		this.notNullCheckBox.setSelected(info.notNull);
		this.autoIncreamentCheckBox.setSelected(info.autoIncrement);
	}
}
